package des.wangku.operate.standard.webserver.control;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import des.wangku.operate.standard.Pv;
import des.wangku.operate.standard.Pv.Env;
import des.wangku.operate.standard.tools.HttpServletRequests;

/**
 * 发送电子邮件<br>
 * 通过form发送
 * 
 * @author Sunjian
 * @version 1.0
 * @since jdk1.8
 */
@Controller
@RequestMapping(value = "/MQ/sendemail")
public class SendEmail {
	/** 日志 */
	static Logger logger = LoggerFactory.getLogger(SendEmail.class);

	@Autowired
	HttpServletRequest request;

	/**
	 * 以 泛型 的方式接收传递值
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return boolean
	 */
	@ResponseBody
	@CrossOrigin(maxAge = 3600)
	@RequestMapping(value = "/sendparadigm", method = RequestMethod.POST)
	public boolean sendFormParadigm(HttpServletRequest request) {
		String sendEmailTitle = request.getParameter("sendEmailTitle");
		String emailContent = Utils.getParadigmEmailContent(request);
		List<String> addressList = HttpServletRequests.getParameterList(request, "sendEmailAddress");
		String[] arr = new String[0];
		if ((Pv.ACC_ENV == Env.DEV)) {
			addressList.clear();
			addressList.add("sunjian@99114.com");
		}
		arr = addressList.toArray(arr);
		String a = sendHtmlMail("1", sendEmailTitle, emailContent, arr);
		if (a == null) return false;
		return a.equals("success");
	}

	/**
	 * 邮件发送接口
	 * 
	 * @param key
	 *            String
	 * @param emailTitle
	 *            String
	 * @param emailContent
	 *            String
	 * @param address
	 *            String[]
	 * @return String
	 */
	public String sendHtmlMail(String key, String emailTitle, String emailContent, String... address) {
		for (String addr : address) {
			if (addr == null || addr.trim().length() == 0) continue;
			// (emailContent, emailTitle, key, add);
		}
		return "success";
	}

	/**
	 * 针对使用泛型邮件输入的参数进行甄别<br>
	 * 
	 * @author Sunjian
	 * @version 1.0
	 * @since jdk1.7
	 */
	public static final class Utils {
		/** 泛型参数名称的尾部关键字 */
		private static final String ACC_PARAPARANAMETAIL = "_title";

		/**
		 * 把所有参数对按参数名顺序输入到emailContent中
		 * 
		 * @param request
		 *            HttpServletRequest
		 * @return String
		 */
		static String getParadigmEmailContent(HttpServletRequest request) {
			StringBuilder sb = new StringBuilder(100);
			List<String> parameterList = HttpServletRequests.getParadigmNameList(request, ACC_PARAPARANAMETAIL);
			Collections.sort(parameterList);
			String key;
			sb.append("<style>.table-b table{border:1px solid #F00}.table-b table td{border:1px solid #F00}</style> ");
			sb.append("<table class='able-b' border='1' cellspacing='0' cellpadding='0'>");
			for (int i = 0, len = parameterList.size(); i < len; i++) {
				sb.append("<tr height='45'>");
				key = parameterList.get(i);
				sb.append("<td width='120' align='right'>" + request.getParameter(key + Utils.ACC_PARAPARANAMETAIL) + "：</td>");
				sb.append("<td width='700'>" + request.getParameter(key) + "</td>");
				sb.append("</tr>");
			}
			sb.append("</table>");
			return sb.toString();
		}
	}
}
